package processes.jurassicPark;

import java.util.Random;

import os.MiniOS;
import os.MiniProcess;
import os.MiniThread;

public class PassengerThread extends MiniThread
{
	private CarStock stock;
	private int passengerNum;
	private int wanderMin;
	private int wanderMax;
	private int shoppingMin;
	private int shoppingMax;

	public PassengerThread(MiniProcess process, String tid)
	{
		super(process, tid);
	}

	public int getPassengerNum()
	{
		return passengerNum;
	}

	@Override
	public void passThreadArguments(Object[] arguments)
	{
		passengerNum = (Integer) arguments[0];
		stock = (CarStock) arguments[1];
		wanderMin = (Integer) arguments[2];
		wanderMax = (Integer) arguments[3];
		shoppingMin = (Integer) arguments[4];
		shoppingMax = (Integer) arguments[5];
	}

	@Override
	public void run()
	{
		Random random = new Random();
		int intervalTime = random.nextInt(wanderMax - wanderMin);
		intervalTime += wanderMin;
		try
		{
			// System.out.println("Passenger no "+passengerNum+" is wondering");
			sleep(intervalTime);
		} catch (InterruptedException e)
		{
			e.printStackTrace();
		}
		stock.takeACar(passengerNum, this);
		System.err.println("Passenger no " + passengerNum + " finished");
		System.err.println("Passenger no " + passengerNum + "want shop");
		GiftShop.getInstance(0).shop(this);
		System.err
				.println("Passenger no " + passengerNum + " finishedShopping");
		MiniOS.getInstance().suspendThread(this);

	}

	public void shopping()
	{
		try
		{
			Random random = new Random();
			int intervalTime = random.nextInt(shoppingMax - shoppingMin);
			sleep(intervalTime);
		} catch (InterruptedException e)
		{
			e.printStackTrace();
		}
	}

}
